package controller;

import java.util.ArrayList;
import java.util.List;
import entities.DbConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import entities.Cerita;
import entities.DaftarCerita;

/**
 *
 * @author rizqi
 */
public class LogicCerita {

    HttpServletRequest request;

    public LogicCerita(HttpServletRequest request) {
        this.request = request;
    }

    public String add() {
        String isi_cerita = request.getParameter("isi_cerita");

        DbConnection dbConn = new DbConnection();
        DaftarCerita mdCerita = new DaftarCerita(dbConn.getConnection());

        try {
            mdCerita.add(isi_cerita);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        //redirect method
        return "listCerita.jsp";

    }

    //select one
    public String selectOnexxx() {
        String id_cerita = request.getParameter("id_cerita");
        List<Cerita> cerita = new ArrayList<Cerita>();
        DbConnection dbConn = new DbConnection();
        DaftarCerita mdCer = new DaftarCerita(dbConn.getConnection());
        try {

            int id = Integer.parseInt(id_cerita);
            cerita = mdCer.selectone(id);
            request.setAttribute("ceritas", cerita);
            //request.setAttribute(null, this);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "listCerita.jsp";
    }

    //one
    public String selectone() {
        String id_cerita = request.getParameter("id_cerita");
        List<Cerita> cerita = new ArrayList<Cerita>();
        DbConnection dbConn = new DbConnection();
        DaftarCerita mdCer = new DaftarCerita(dbConn.getConnection());
        try {

            int id = Integer.parseInt(id_cerita);
            cerita = mdCer.selectone(id);
            request.setAttribute("ceritas", cerita);
            //request.setAttribute(null, this);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "cerita_update.jsp";
    }

    public String update() {

        String id_cerita = request.getParameter("id_cerita");
        String id_propinsi = request.getParameter("id_propinsi");
        String isi_cerita = request.getParameter("isi_cerita");

        DbConnection dbConn = new DbConnection();
        DaftarCerita mdCerita = new DaftarCerita(dbConn.getConnection());


        try {
            Long id = Long.parseLong(id_cerita);
            mdCerita.update(id, id_propinsi, isi_cerita);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return listCerita();
    }

    //list barang Bos
    public String listCerita() {
        //String idBarang = request.getParameter("idBarang");
        List<Cerita> cerita = new ArrayList<Cerita>();
        DbConnection dbConn = new DbConnection();
        DaftarCerita mdCer = new DaftarCerita(dbConn.getConnection());
        try {

            cerita = mdCer.getAllCerita();
            request.setAttribute("ceritas", cerita);
            //request.setAttribute(null, this);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "listCerita.jsp";
    }

    public String delete() {
        String id_cerita = request.getParameter("id_cerita");

        DbConnection dbConn = new DbConnection();
        DaftarCerita mdCer = new DaftarCerita(dbConn.getConnection());
        try {


            Long id = Long.parseLong(id_cerita);
            mdCer.delete(id);



        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return listCerita();
    }
}
